from django import forms
from django.forms import ModelForm, Textarea
from django.forms.widgets import HiddenInput
from django.db.models import Q, Count
from relationships.models import RelationshipStatus, Relationship


class RelationshipStatusAdminForm(ModelForm):
    
    class Meta:
        model = RelationshipStatus
        
    def duplicate_name_check(self, status_name):
        status_name_qs = RelationshipStatus.objects.filter(Q(name=status_name))
        
        if self.instance.pk:
            status_name_qs = status_name_qs.exclude(pk=self.instance.pk)

        if status_name_qs.exists():
            raise forms.ValidationError('"%s" already in use on %s' % \
                (status_name, unicode(status_name_qs[0])))

    def duplicate_slug_check(self, status_slug):
        status_qs = RelationshipStatus.objects.filter(
            Q(from_slug=status_slug) |
            Q(to_slug=status_slug) |
            Q(symmetrical_slug=status_slug)
        )

        if self.instance.pk:
            status_qs = status_qs.exclude(pk=self.instance.pk)

        if status_qs.exists():
            raise forms.ValidationError('"%s" slug already in use on %s' % \
                (status_slug, unicode(status_qs[0])))
            
    def clean_name(self):
        self.duplicate_name_check(self.cleaned_data['name'])
        return self.cleaned_data['name']

    def clean_from_slug(self):
        self.duplicate_slug_check(self.cleaned_data['from_slug'])
        return self.cleaned_data['from_slug']

    def clean_to_slug(self):
        self.duplicate_slug_check(self.cleaned_data['to_slug'])
        return self.cleaned_data['to_slug']

    def clean_symmetrical_slug(self):
        self.duplicate_slug_check(self.cleaned_data['symmetrical_slug'])
        return self.cleaned_data['symmetrical_slug']

    def clean(self):
        if self.errors:
            return self.cleaned_data

        if RelationshipStatus.objects.filter(name=self.cleaned_data['name']).count() > 1 or\
           self.cleaned_data['from_slug'] == self.cleaned_data['to_slug'] or \
           self.cleaned_data['to_slug'] == self.cleaned_data['symmetrical_slug'] or \
           self.cleaned_data['symmetrical_slug'] == self.cleaned_data['from_slug']:
            raise forms.ValidationError('from, to, and symmetrical slugs must be different')

        return self.cleaned_data
        
class RelationshipStatusForm(RelationshipStatusAdminForm):
    """
    This class is Inheritance form RelationshipStatusAdminForm
    """
            
    # input hidden
    def __init__(self, *args, **kwargs):
        super(RelationshipStatusForm, self).__init__(*args, **kwargs)
        self.fields['owner'].widget = HiddenInput()
        

class RelationshipAdminForm(ModelForm):
    class Meta:
        model = Relationship
    
    def clean(self):
        if self.errors:
            return self.cleaned_data

        if self.cleaned_data['from_user'] == self.cleaned_data['to_user']:
            raise forms.ValidationError('From user & To user must be different')

        return self.cleaned_data
    
class RelationshipForm(RelationshipAdminForm):
    """
    This class is Inheritance form RelationshipAdminForm
    """
            
    # input hidden
    def __init__(self, *args, **kwargs):
        super(RelationshipForm, self).__init__(*args, **kwargs)
        self.fields['from_user'].widget = HiddenInput()
